home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Palettes / TTools / TToolsPalette / TBinderList.subproj / SelPairAgent.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  3.3 KB  |  117 lines

  1. /* SelPairAgent.m
  2.  * Written By:  Thomas Burkholder
  3.  *
  4.  * You may freely copy, distribute, and reuse the code in this example.
  5.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  6.  * fitness for any particular use.
  7.  */
  8.  
  9. #import "../Utilities.subproj/ClassAdditions.h"
  10. #import "SelectorBrowsingFunctions.h"
  11. #import "SelPairAgent.h"
  12. #import "TBinderListConnectInspector.h"
  13. #import <objc/objc-class.h>
  14. #import <apps/InterfaceBuilder.h>
  15.  
  16. #define T_UNKNOWN 0
  17. #define T_ORDINAL 1
  18. #define T_STRING 2
  19. #define T_OBJECT 3
  20. #define T_SELECTOR 4
  21.  
  22. int abstractType(char c)
  23. {
  24.     if ((c==*@encode(char)) || (c==*@encode(int)) ||
  25.         (c==*@encode(short)) || (c==*@encode(long)) ||
  26.         (c==*@encode(unsigned char)) || (c==*@encode(unsigned int)) ||
  27.         (c==*@encode(unsigned short)) || (c==*@encode(unsigned long)) ||
  28.         (c==*@encode(float)) || (c==*@encode(double)))
  29.         return T_ORDINAL;
  30.     if (c==*@encode(char *)) return T_STRING;
  31.     if (c==*@encode(id)) return T_OBJECT;
  32.     if (c==*@encode(SEL)) return T_SELECTOR;
  33.     return T_UNKNOWN;
  34. }
  35.  
  36. void filterForReturnTypes(id storage, id srcClass, SEL srcSelector,id destClass)
  37. {
  38.     // Icky, nasty.  storage is assumed to contain SEL's.  srcClass and
  39.     // srcSelector give us the templateArgs - the arg types of the
  40.     // srcSelector method in the srcClass class.  destClass we need, in
  41.     // order to get the arg types of each selector in the storage.
  42.     int i,tp1,tp2;
  43.  
  44.     // we get the arg types from class and aSelector:
  45.     struct objc_method *m = class_getInstanceMethod(srcClass,srcSelector);
  46.     const char *candidateArgs, *templateArgs = m->method_types;
  47.  
  48.     // eliminate all selectors whose return values don't match.
  49.  
  50.     for(i=0;(i<[storage count]);) {
  51.         m = class_getInstanceMethod(destClass,*(SEL *)[storage elementAt:i]);
  52.         candidateArgs = m->method_types;
  53.         // Some primitive type-matching, taking advantage of type
  54.         // coersion...
  55.         tp1 = abstractType(candidateArgs[0]);
  56.         tp2 = abstractType(templateArgs[0]);
  57.         if ((candidateArgs[0]==templateArgs[0])||(tp1&&tp2&&(tp1==tp2)))
  58.             i++;
  59.         else
  60.             [storage removeElementAt:i];
  61.     }
  62. }
  63.  
  64. @implementation SelPairAgent
  65.  
  66. - initFrom:anObject
  67. {
  68.     // A bit crude; we use initialStorage to determine leafdom.
  69.     [super init];
  70.     initialStorage = anObject;
  71.     secondaryStorage = [[SortedStorage alloc] init];
  72.     [secondaryStorage setAgent:self];
  73.     return self;
  74. }
  75.  
  76. - free
  77. {
  78.     [secondaryStorage free];
  79.     return [super free];
  80. }
  81.  
  82. - subdirectoryFor:(void *)anElement sender:sender
  83. {
  84.     if (sender != initialStorage || ![NXApp isConnecting])
  85.         return nil;
  86.     [[[NXApp connectDestination] class] methodSelectors:secondaryStorage
  87.                             includeAncestors:YES
  88.                             filterWith:isAccessor];
  89.     // match the return type of the selected SEL.
  90.     filterForReturnTypes(secondaryStorage,
  91.                 [[getDSConnectorForSource([NXApp connectSource]) destination] class],
  92.                 *(SEL *)anElement,
  93.                 [[NXApp connectDestination] class]);
  94.     return secondaryStorage;
  95. }
  96.  
  97. - (BOOL)isLeaf:(void *)anElement sender:sender
  98. {
  99.     return (![NXApp isConnecting] || sender != initialStorage);
  100. }
  101.  
  102. - (const char *)displayStringFor:(void *)anElement sender:sender
  103. {
  104.     return sel_getName(*(SEL *)anElement);
  105. }
  106.  
  107. - (int)compare:(void *)first with:(void *)second sender:sender
  108. {
  109.     return strcmp(sel_getName(*(SEL *)first), sel_getName(*(SEL *)second));
  110. }
  111.  
  112. - (const char *)titleOfColumn:(int)col
  113. {
  114.     return (col)?"Interface":"DataSource";
  115. }
  116.  
  117. @end